package com.lin.subject.domain.handler.subject;

import cn.hutool.core.collection.ListUtil;
import com.lin.subject.commom.enums.IsDeletedFlagEnum;
import com.lin.subject.commom.enums.SubjectInfoTypeEnum;
import com.lin.subject.domain.convert.JudgeSubjectConverter;
import com.lin.subject.domain.convert.MultipleSubjectConverter;
import com.lin.subject.domain.convert.SubjectInfoConvert;
import com.lin.subject.domain.entity.SubjectAnswerBO;
import com.lin.subject.domain.entity.SubjectInfoBO;
import com.lin.subject.domain.entity.SubjectOptionBO;
import com.lin.subject.infra.basic.entity.SubjectJudge;
import com.lin.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/*
 * @Author Lin
 * @Description 判断题实现类
 * @Date 2025/2/7
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectJudgeService judgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    /**
     * 实际题目的插入
     *
     * @param subjectInfoBo
     */
    @Override
    public void insertSubject(SubjectInfoBO subjectInfoBo) throws Exception {
        // 插入选项
        List<SubjectAnswerBO> optionList = subjectInfoBo.getOptionList();
        if(optionList.isEmpty()){
            throw new Exception("插入题目选项数据为空");
        }
        SubjectAnswerBO one = optionList.get(0);
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setIsCorrect(one.getIsCorrect());
        subjectJudge.setSubjectId(subjectInfoBo.getId());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        judgeService.insertOne(subjectJudge);
    }

    /**
     * 查询题目的选项及正确答案
     * 找到题目信息
     * @param id
     * @return
     */
    @Override
    public SubjectOptionBO query(Long id) {
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();

        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(id);
        List<SubjectJudge> judgeList =  judgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConverter.INSTANCE.convertEntityToBoList(judgeList);
        subjectOptionBO.setOptionList(subjectAnswerBOList);

        return subjectOptionBO;
    }

    /**
     * 删除题目
     *
     * @param id
     */
    @Override
    public void deleteSubject(Long id) {
        judgeService.deleteSubject(id);
    }
}
